<?php

declare(strict_types=1);

namespace app\index\controller;

use think\facade\View;
use think\response\Json;
use app\common\model\Draw as DrawModel;
use app\common\model\DrawFd;
use think\exception\ValidateException;

class Draw extends Base
{

    public function index()
    {
        return View::fetch();
    }
    public function index_fd()
    {
        return View::fetch();
    }
    public function mode()
    {
        return View::fetch();
    }

    public function record()
    {
        return View::fetch();
    }

    public function getList(): Json
    {
        $param = $this->request->param();

        $map[] = ['uid', '=', $this->user->id];

        if (!isset($param['account'])) {
            return json(['code' => -1, 'msg' => '参数错误']);
        }
        if ($param['account'] == 'bonus_3') {
            return json(DrawFd::getPageList($map, '*', $param['page'], 20, false));
        } else {
            return json(DrawModel::getPageList($map, '*', $param['page'], 20, false));
        }
    }

    public function save(): Json
    {
        $param = $this->request->post();
        try {
            if (!isset($param['account'])) return json(['code' => -1, 'msg' => '参数错误']);
            if ($param['account'] == 'bonus_3') {
                $this->validate($param, 'DrawFd');
                return json(DrawFd::add($param, $this->user, $this->system));
            } else {
                $this->validate($param, 'Draw');
                return json(DrawModel::add($param, $this->user, $this->system));
            }
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
    }
}
